<div class="wrap">
	<div class="header">
		<a href="javascript:void(0)" id="addBtn">添加设备</a>
	</div>
	<table class="table">
		<thead>
			<tr>
				<th>设备编号</th>
				<th>设备名称</th>
				<th>设备负责人</th>
				<th>操作</th>
			</tr>
		</thead>
		<tbody id="orderCont">
		</tbody>
	</table>
</div>
<!--遮罩层-->
<div class="layout">
	<div class="alert">
		<h3>添加设备</h3>
		<p>
			<label>设备编号</label>
			<input type="text" id="num">
		</p>
		<p>
			<label>设备名称</label>
			<input type="text" id="name">
		</p>
		<p>
			<label>设备负责人</label>
			<select id="person">
				
			</select>
		</p>
		<p>
			<a href="javascript:void(0)" id="submitBtn">提交</a>
			<a href="javascript:void(0)" class="curr" id="cancelBtn">取消</a>
		</p>
	</div>
</div>
<script>
	//展示设备
	$.ajax({
		type:"post",
		url:"http://39.108.164.20/device/SysUser/SelDevice",
		dataType:"json",
		success:function(res){
			$("#orderCont").empty();
			var data = res.result.data;
			var html;
			for(let i=0;i<data.length;i++){
				html += '<tr data-id="'+data[i].sb_id+'"><td>'+data[i].sb_id+'</td><td>'+data[i].sb_name+'</td><td>'+data[i].pet_name+'</td><td><a href="javascript:void(0)" onclick="delFun($(this))">删除</a></td></tr>';
			}
			$("#orderCont").append(html);
		}
	});
	//添加设备负责人
	$.ajax({
		type:"post",
		url:"http://39.108.164.20/device/SysUser/SelPrintList",
		dataType:"json",
		success:function(res){
			$("#person").empty();
			var data1 = res.result.data.afterPrint;
			var data2 = res.result.data.prePrint;
			var data3 = res.result.data.print;
			var data4 = res.result.data.test;
			var html;
			for(let i=0;i<data1.length;i++){
				html += '<option value="'+data1[i].uuid+'">'+data1[i].pet_name+'</option>';
			}
			for(let i=0;i<data2.length;i++){
				html += '<option value="'+data2[i].uuid+'">'+data2[i].pet_name+'</option>';
			}
			for(let i=0;i<data3.length;i++){
				html += '<option value="'+data3[i].uuid+'">'+data3[i].pet_name+'</option>';
			}
			for(let i=0;i<data4.length;i++){
				html += '<option value="'+data4[i].uuid+'">'+data4[i].pet_name+'</option>';
			}
			$("#person").append(html);
		}
	});
	//取消弹窗
	$("#cancelBtn").on("click",function(){
		$(".layout").hide();
		$(".alert").hide();
	})
	//添加设备
	$("#addBtn").on("click",function(){
		$(".layout").show();
		$(".alert").show();
	})
	//确认
	$("#submitBtn").on("click",function(){
		var sb_id  = $("#num").val(),
			name   = $("#name").val(),
			person = $("#person").val();
		$.ajax({
			type:"post",
			url:"http://39.108.164.20/device/SysUser/InsDevice",
			data:{
				sb_id:sb_id,
				name:name,
				person:person
			},
			dataType:"json",
			success:function(res){
				$(".layout").hide();
				$(".alert").hide();
				if(res.result.success == 0){
					alert("添加成功！")
					//展示设备
					$.ajax({
						type:"post",
						url:"http://39.108.164.20/device/SysUser/SelDevice",
						dataType:"json",
						success:function(res){
							$("#orderCont").empty();
							var data = res.result.data;
							var html;
							for(let i=0;i<data.length;i++){
								html += '<tr data-id="'+data[i].sb_id+'"><td>'+data[i].sb_id+'</td><td>'+data[i].sb_name+'</td><td>'+data[i].pet_name+'</td><td><a href="javascript:void(0)" onclick="delFun($(this))">删除</a></td></tr>';
							}
							$("#orderCont").append(html);
						}
					});
				}
			}
		});	
	})
	//删除方法
	function delFun(e){
		var sb_id = e.parents("tr").attr("data-id");
		if(confirm("您确定要删除吗？")){
			$.ajax({
				type:"post",
				url:"http://39.108.164.20/device/SysUser/DelOrder",
				dataType:"json",
				data:{
					sb_id:sb_id,
					type:"delDevice"
				},
				success:function(res){
					console.log(res)
					if(res.result.success==0){
						alert("删除成功！");
						//展示设备
						$.ajax({
							type:"post",
							url:"http://39.108.164.20/device/SysUser/SelDevice",
							dataType:"json",
							success:function(res){
								$("#orderCont").empty();
								var data = res.result.data;
								var html;
								for(let i=0;i<data.length;i++){
									html += '<tr><td>'+data[i].sb_id+'</td><td>'+data[i].sb_name+'</td><td>'+data[i].pet_name+'</td><td><a href="javascript:void(0)" onclick="delFun($(this))">删除</a></td></tr>';
								}
								$("#orderCont").append(html);
							}
						});
					}
				}
			});
		}
	}
</script>